#pragma once
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string>
#include <cstring>
#include <cstdlib>
#include <pthread.h>
#include "log.hpp"
#include "Task.hpp"
#include "ThreadPool.hpp"
namespace Server
{
    enum
    {
        USAGE_ERR = 1,
        SOCKET_ERR,
        BIND_ERR,
        LISTEN_ERR,
    };
    static const uint16_t gport = 8080;             // 缺省的端口号
    static const int gbacklog = 5;                  // 最大连接数=5+1
    const static std::string defaultIp = "0.0.0.0"; // 缺省的IP
    class TcpServer;
    struct ThreadData // 用于线程函数传参
    {
        ThreadData(TcpServer *self, const int &sock)
            : _self(self), _sock(sock)
        {
        }
        TcpServer *_self; // this
        int _sock;        // 通信fd
    };
    class TcpServer
    {
    public:
        TcpServer(const uint16_t &port = gport, const std::string &ip = defaultIp)
            : _listenSocket(-1), _port(port), _ip(ip)
        {
        }
        void InitServer() // 初始化服务器
        {
            // 1、创建sockrt套接字
            _listenSocket = socket(AF_INET, SOCK_STREAM, 0);
            if (_listenSocket < 0)
            {
                LogMessage(FATAL, "create socket error");
                exit(SOCKET_ERR);
            }
            LogMessage(NORMAL, "create socket success");
            // 2、绑定端口号+ip地址
            struct sockaddr_in local;
            memset(&local, 0, sizeof(local));
            local.sin_addr.s_addr = inet_addr(_ip.c_str());
            local.sin_family = AF_INET;
            local.sin_port = htons(_port);
            if (bind(_listenSocket, (struct sockaddr *)&local, sizeof(local)) < 0)
            {
                LogMessage(FATAL, "bind socket error");
                exit(BIND_ERR);
            }
            LogMessage(NORMAL, "bind socket success");
            // 3、设置监听状态
            if (-1 == listen(_listenSocket, gbacklog))
            {
                LogMessage(FATAL, "listen socket error");
                exit(LISTEN_ERR);
            }
            LogMessage(NORMAL, "listen socket success");
        }
        void Start() // 启动服务器
        {
            // 4、线程池初始化
            ThreadPool<Task>::getInstance()->run(); // 线程启动
            while (1)
            {
                // 5、服务器获取客户端连接请求
                struct sockaddr_in peer; // 输出型参数，拿到客户端的信息
                socklen_t len = sizeof(peer);
                int sock = accept(_listenSocket, (struct sockaddr *)&peer, &len);
                if (-1 == sock)
                {
                    LogMessage(ERROR, "accept error,next");
                    continue;
                }
                LogMessage(NORMAL, "accept a new link success");
                ThreadPool<Task>::getInstance()->push(Task(sock, ServerIO));
            }
        }
        ~TcpServer()
        {
        }

    private:
        int _listenSocket; // 监听客户端的连接请求，不用于数据通信
        uint16_t _port;    // 服务器端口号
        std::string _ip;   // 服务器ip地址
    };
}